SPB Git
1.0 KB · 14 lines typescript
1import { createReadStream, promises as fs } from "node:fs";2import { Readable } from "node:stream";3import { join } from "node:path";4import { previewFor } from "@/lib/papers/queries";5import { STORAGE_DIR } from "@/lib/env";6export const dynamic = "force-dynamic";7/** First-page preview of the current manuscript: /api/preview/<slug>.png */8export async function GET(_req: Request, { params }: { params: Promise<{ name: string }> }) {9  const { name } = await params; const slug = name.replace(/\.png$/, ""); if (!/^[a-z0-9]+$/.test(slug)) return new Response("Not found", { status: 404 });10  const sha = await previewFor(slug); if (!sha) return new Response("Not found", { status: 404 });11  const path = join(STORAGE_DIR, "previews", `${sha}.png`); try { await fs.access(path); } catch { return new Response("Not found", { status: 404 }); }12  return new Response(Readable.toWeb(createReadStream(path)) as unknown as ReadableStream, { headers: { "Content-Type": "image/png", "Cache-Control": "public, max-age=604800, immutable", ETag: `"${sha}"` } });13}14
Files · main
Menu

Theme